@model LanguageModel

<div class="panel-group">
    <div class="sub-panel panel-search">
        <div class="panel-body">
            <div class="row">
                <div class="col-md-6">
                    @if (Model.Id > 0)
                    {
                        <div class="form-group">
                            <div class="col-md-3">
                                <nop-label asp-for="LocaleResourceSearchModel.SearchResourceName" />
                            </div>
                            <div class="col-md-9">
                                <nop-editor asp-for="LocaleResourceSearchModel.SearchResourceName" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-3">
                                <nop-label asp-for="LocaleResourceSearchModel.SearchResourceValue" />
                            </div>
                            <div class="col-md-9">
                                <nop-editor asp-for="LocaleResourceSearchModel.SearchResourceValue" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-9 col-md-offset-4">
                                <button type="button" id="search-resources" class="btn btn-primary btn-search">
                                    <i class="fa fa-search"></i>
                                    @T("Admin.Common.Search")
                                </button>
                            </div>
                        </div>
                    }
                    else
                    {
                        <div class="form-group">
                            <div class="col-md-12">
                                @T("Admin.Configuration.Languages.Resources.SaveBeforeEdit")
                            </div>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>

    @if (Model.Id > 0)
    {
        <div class="sub-panel">
            <div class="panel-body">
                @await Html.PartialAsync("Table", new DataTablesModel
                {
                    Name = "resources-grid",
                    UrlRead = new DataUrl("Resources", "Language", new RouteValueDictionary { [nameof(Model.LocaleResourceSearchModel.LanguageId)] = Model.Id }),
                    UrlUpdate = new DataUrl("ResourceUpdate", "Language", null),
                    UrlDelete = new DataUrl("ResourceDelete", "Language", null),
                    SearchButtonId = "search-resources",
                    Length = Model.LocaleResourceSearchModel.PageSize,
                    LengthMenu = Model.LocaleResourceSearchModel.AvailablePageSizes,
                    Filters = new List<FilterParameter>
                    {
                        new FilterParameter(nameof(Model.LocaleResourceSearchModel.SearchResourceName), nameof(LocaleResourceSearchModel)),
                        new FilterParameter(nameof(Model.LocaleResourceSearchModel.SearchResourceValue), nameof(LocaleResourceSearchModel))
                    },
                    ColumnCollection = new List<ColumnProperty>
                    {
                        new ColumnProperty(nameof(LocaleResourceModel.ResourceName))
                        {
                            Title = T("Admin.Configuration.Languages.Resources.Fields.Name").Text,
                            Width = "300",
                            Editable = true,
                            EditType = EditType.String
                        },
                        new ColumnProperty(nameof(LocaleResourceModel.ResourceValue))
                        {
                            Title = T("Admin.Configuration.Languages.Resources.Fields.Value").Text,
                            Editable = true,
                            EditType = EditType.String
                        },
                        new ColumnProperty(nameof(LocaleResourceModel.Id))
                        {
                            Title = T("Admin.Common.Edit").Text,
                            Width = "100",
                            ClassName =  NopColumnClassDefaults.Button,
                            Render = new RenderButtonsInlineEdit()
                        },
                        new ColumnProperty(nameof(LocaleResourceModel.Id))
                        {
                            Title = T("Admin.Common.Delete").Text,
                            Width = "100",
                            ClassName =  NopColumnClassDefaults.Button,
                            Render = new RenderButtonRemove(T("Admin.Common.Delete").Text)
                        }
                    }
                })
                <div class="panel panel-default no-margin">
                    <div class="panel-heading">
                        @T("Admin.Common.AddNewRecord")
                    </div>
                    <div class="panel-body">
                        <div class="form-group">
                            <div class="col-md-3">
                                <nop-label asp-for="@Model.LocaleResourceSearchModel.AddResourceString.ResourceName" />
                            </div>
                            <div class="col-md-9">
                                <nop-editor asp-for="@Model.LocaleResourceSearchModel.AddResourceString.ResourceName" />
                                <span asp-validation-for="@Model.LocaleResourceSearchModel.AddResourceString.ResourceName"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-3">
                                <nop-label asp-for="@Model.LocaleResourceSearchModel.AddResourceString.ResourceValue" />
                            </div>
                            <div class="col-md-9">
                                <nop-editor asp-for="@Model.LocaleResourceSearchModel.AddResourceString.ResourceValue" />
                                <span asp-validation-for="@Model.LocaleResourceSearchModel.AddResourceString.ResourceValue"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-md-9 col-md-offset-3">
                                <button type="button" id="addResourceString" class="btn btn-primary">@T("Admin.Common.AddNewRecord")</button>
                            </div>
                        </div>
                    </div>
                    <script>
                    $(document).ready(function () {
                        $('#addResourceString').click(function () {
                            $('#addResourceString').attr('disabled', true);
                            var postData = {
                                ResourceName: $("#@Html.IdFor(model => model.LocaleResourceSearchModel.AddResourceString.ResourceName)").val(),
                                ResourceValue: $("#@Html.IdFor(model => model.LocaleResourceSearchModel.AddResourceString.ResourceValue)").val()
                            };
                            addAntiForgeryToken(postData);

                            $.ajax({
                                cache: false,
                                type: "POST",
                                url: "@Html.Raw(Url.Action("ResourceAdd", "Language", new { languageId = Model.Id }))",
                                data: postData,
                                success: function (data, textStatus, jqXHR) {
                                    if (data.Result) {
                                        //reload grid
                                        updateTable('#resources-grid');

                                        //clear input value
                                        $("#@Html.IdFor(model => model.LocaleResourceSearchModel.AddResourceString.ResourceName)").val('');
                                        $("#@Html.IdFor(model => model.LocaleResourceSearchModel.AddResourceString.ResourceValue)").val('');
                                    } else {
                                        //display errors if returned
                                        display_nop_error(data);
                                    }
                                },
                                complete: function (jqXHR, textStatus) {
                                    $('#addResourceString').attr('disabled', false);
                                }
                            });
                        });
                    });
                    </script>
                </div>
            </div>
        </div>
    }
</div>
